﻿using System;
using System.Collections.Generic;
using System.Linq;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Shared.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class UserMessageSenderControlViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        IMembershipRepository membershipRepository;
        /// <summary>
        /// 
        /// </summary>
        IUserMessagesRepository messagesRepository;
        /// <summary>
        /// Initializes a new instance of the <see cref="UserMessageSenderControlViewModel"/> class.
        /// </summary>
        /// <param name="memberRepo">The member repo.</param>
        /// <param name="messageRepo">The message repo.</param>
        public UserMessageSenderControlViewModel(IMembershipRepository memberRepo, IUserMessagesRepository messageRepo)
        {
            this.membershipRepository = memberRepo;
            this.messagesRepository = messageRepo;
        }

        /// <summary>
        /// Gets all users.
        /// </summary>
        /// <param name="CommitteeID">The committee ID.</param>
        /// <returns></returns>
        public List<IUser> GetAllUsers(Guid CommitteeID)
        {
            return membershipRepository.FindUsers(u => u.UsersCommittees.Select(usecom => usecom.CommitteeID).Contains(CommitteeID)).ToList();
        }

        /// <summary>
        /// Sends the message.
        /// </summary>
        /// <param name="toUser">To user.</param>
        /// <param name="txtSubject">The TXT subject.</param>
        /// <param name="txtMessage">The TXT message.</param>
        public void SendMessage(Guid toUser, string txtSubject, string txtMessage)
        {


            using (IUnitOfWork unit = this.GetInstance<IUnitOfWork>())
            {
                IUserMessage message = this.GetInstance<IUserMessage>();
                message.UserMessageID = Guid.NewGuid();
                message.ToUserID = toUser;
                message.FromUserID = RegistryInfo.UserID;
                message.Subject = txtSubject;
                message.Message = txtMessage;
                message.CreationDate = DateTime.Now;
                messagesRepository.CreateUserMessage(message);

                unit.Commit();
            }
        }
    }
}
